/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-07-26 17:45:28
 * Copyright (c) 2015- -版权所有
 */
package com.joysuch.wwyt.bp.repository;

import com.joysuch.wwyt.bp.entity.BpDeviceFacilityMaintainRecord;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

import java.util.Date;


/**
 * BpDeviceFacilityMaintainRecordDao
 *
 */
public interface BpDeviceFacilityMaintainRecordDao extends JpaRepository<BpDeviceFacilityMaintainRecord, Long>, QuerydslPredicateExecutor<BpDeviceFacilityMaintainRecord> {
    public Page<BpDeviceFacilityMaintainRecord> findAll(Specification<BpDeviceFacilityMaintainRecord> spec, Pageable pageable);

    public BpDeviceFacilityMaintainRecord save(BpDeviceFacilityMaintainRecord bean);

    public void delete(BpDeviceFacilityMaintainRecord bean);

    int countByDeviceFacilityId(Long deviceFacilityId);

    @Query(value = "SELECT COUNT(*) FROM wwyt_bp_device_facility_maintain_record WHERE DEVICE_FACILITY_ID =?1 AND ?2 BETWEEN MAINTAIN_DATE AND maintain_end_date  AND delete_flag = '0'", nativeQuery = true)
    int countByDeviceFacilityIdAndNowDate(Long facilityId, Date date);

    @Query(value = "select count(*) from wwyt_bp_device_facility_maintain_record where id != ?1 and device_facility_id = ?2 and maintain_end_date >= ?3 and delete_flag = '0' ", nativeQuery = true)
    Long isLastRecord(Long id, Long deviceFacilityId, Date maintainEndDate);
    
}
